package com.sky.poi;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.Iterator;

public class ApachePoi {
    public static  void write() throws IOException {
        //在内存中创建一个excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建一个表单
        XSSFSheet sheet = workbook.createSheet();
        //创建行
        XSSFRow row = sheet.createRow(1);
        //创建列
        XSSFCell cell = row.createCell(1);
        cell.setCellValue("姓名");
        XSSFCell cell1 = row.createCell(2);
        cell1.setCellValue("爱好");
        XSSFRow row1 = sheet.createRow(2);
        row1.createCell(1).setCellValue("小明");
        row1.createCell(2).setCellValue("跳舞");
        XSSFRow row2 = sheet.createRow(3);
        row2.createCell(1).setCellValue("小达");
        row2.createCell(2).setCellValue("篮球");
        File file = new File("E:\\BaiduNetdiskDownload\\资料\\day12\\info.xlsx");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            workbook.close();
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
    public static void  read() throws IOException {
        FileInputStream fin = new FileInputStream("E:\\BaiduNetdiskDownload\\资料\\day12\\info.xlsx");
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fin);
        XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheetAt.rowIterator();
        while (rowIterator.hasNext()){
            Row next = rowIterator.next();
            Iterator<Cell> cellIterator = next.cellIterator();
            while (cellIterator.hasNext()){
                Cell next1 = cellIterator.next();
                System.out.print(next1+"   ");
            }
            System.out.println();
        }

    }
    public static void main(String[] args) throws IOException {
//        write();
        read();
    }
}
